package acm;

import java.io.PrintStream;
import java.util.*;

public class ChemicalFind
{
	PrintStream out;
	Scanner in;
	
	ArrayList<String> cheHave;
	ArrayList<Recation> recation;
	ArrayList<String> newThing;
	
	static class Recation
	{
		ArrayList<String> before;
		ArrayList<String> after;
		
		public Recation(String recation)
		{
			before = new ArrayList<String>();
			after = new ArrayList<String>();
			
			String[] rec = recation.split("=");
			
			for ( String i : rec[0].split("\\+"))
			{
				before.add(i);
			}
			
			for ( String i : rec[1].split("\\+"))
			{
				after.add(i);
			}
		}
	}
	
	public ChemicalFind()
	{
		out = System.out;
		in = new Scanner(System.in);
		
		cheHave = new ArrayList<String>();
		newThing = new ArrayList<String>();
		recation = new ArrayList<Recation>();
	}
	
	public void run()
	{
		int i = in.nextInt();
		
		for (  ; i>0; i--)
		{
			recation.add(new Recation(in.next()));
		}
		
		i = in.nextInt();
		
		for ( ; i>0; i--)
		{
			cheHave.add(in.next());
		}
		
		figure();
		
		out.println(newThing.size());
		for(String str : newThing)
		{
			out.println(str);
		}
	}
	
	private void figure()
	{
		boolean contain = true;
		Recation recTemp = null;
		
		for ( Recation rec : recation )
		{
			contain = true;
			
			for ( String bef : rec.before)
			{
				if ( !cheHave.contains(bef) )
				{
					contain = false;
					break;
				}
			}
			
			if (contain == true)
			{
				for ( String aft : rec.after )
				{
					if ( !cheHave.contains(aft))
					{
						myAdd(newThing, aft);
						cheHave.add(aft);
					}
				}
				
				recTemp = rec;
				
				break;
			}
		}
		
		if (contain == true)
		{
			recation.remove(recTemp);
			
			figure();
		}
	}
	
	private void myAdd(ArrayList<String> newThing2, String aft)
	{
		int length = newThing2.size();
		
		for(int i=0; i<length; i++)
		{
			for(int j=0; j<10; j++)
			{
				if (getChar(aft, j) < getChar(newThing2.get(i), j))
				{
					newThing2.add(i, aft);
					return ;
				}
				else if(getChar(aft, j) > getChar(newThing2.get(i), j))
				{
					break;
				}
			}
		}
		
		newThing2.add(aft);
	}

	private int getChar(String aft, int index)
	{
		if ( index < aft.length() )
		{
			return (int)aft.charAt(index);
		}
		else
		{
			return 0;
		}
	}

	public static void main(String[] args)
	{
		ChemicalFind ch = new ChemicalFind();
		
		ch.run();
	}
}
